<template>
	<view>

		<view v-if="markers.length > 0">



			<view class="item" v-for="(item, index) in markers" :key="index">


				<view class="text-line" style="margin-top: 30upx;">
					<text class="text-name"
						style="font-weight: bold;font-size: 32upx;color: #333333;">{{item.mainName}}</text>
				</view>


				<view class="text-line">
					<text class="text-name"
						style="font-size: 28upx;color: #a7a7a7;margin-right: 20upx;white-space: normal;">{{filterNull(item.distanceDesc)+ ' | ' +item.mainAddress}}</text>
				</view>


				<view class="title-wrap">

					<view class="title-warn">

					</view>

					<view class="btn-detail navi-wrap" @click.stop="navi(item)">
						<image class="navi-img" src="/static/home/navigation_icon.png" mode="widthFix">
						</image>
						到这去
					</view>
				</view>

			</view>

			<!-- <u-loadmore :status="loadingStatus" @loadmore="clickloadmore"></u-loadmore> -->
		</view>

		<no-data v-else></no-data>

	</view>
</template>

<script>
	import {
		getMainPlace
	} from '@/common/api.js';
	
	import {bMapToQQMap} from '@/util/index.js';


	export default {
		data() {
			return {
				pageNo: 0,
				loadingStatus: 'loading',
				myLat: '',
				myLng: '',
				markers: [],
				type: 0
			}
		},

		onLoad(op) {
			this.type = op.type;

			if (0 == this.type) {
				uni.setNavigationBarTitle({
					title: '维修点地址'
				})
			} else if (1 == this.type) {
				uni.setNavigationBarTitle({
					title: '保养点地址'
				})
			}

			// uni.getSetting({
			// 	success: (res) => {
			// 		console.log('第一个成功');
			// 		if (res.authSetting['scope.userLocation'] != undefined && res.authSetting[
			// 				'scope.userLocation'] != true) { //非初始化进入该页面,且未授权
			// 			console.log('未授权，走授权');
			// 			uni.openSetting({
			// 				success: function(data) {
			// 					if (data.authSetting["scope.userLocation"] == true) {
			// 						uni.showToast({
			// 							title: '授权成功',
			// 							icon: 'success',
			// 							duration: 2000
			// 						})
			// 						//再次授权，调用getLocationt的API
			// 						console.log('授权成功了开始走init方法');
			// 						// this.getInit()
			// 						this.initMap();
			// 					} else {
			// 						uni.showToast({
			// 							title: '授权失败',
			// 							icon: 'none',
			// 							duration: 2000
			// 						})
			// 					}
			// 				}
			// 			})
			// 		} else { //初始化进入
			// 			console.log('本来就有授权开始走init方法');
			// 			this.initMap();
			// 			this.getsearchdata();
			// 		}
			// 	},

			// 	fail: (res) => {
			// 		console.log('失败', res);
			// 	}
			// })

			this.initMap();

		},

		methods: {

			initMap() {
				console.log('initmap进入了');

				wx.getLocation({
					type: 'wgs84',
					success: (res) => {
						this.myLat = res.latitude;
						this.myLng = res.longitude;

						console.log('mylat', res);
						this.getsearchdata();
					},
					fail: (e) => {
						console.log('获取定位失败', e);
						this.getsearchdata();
					}
				})

				// this.getsearchdata();
			},

			// 读取数据
			getsearchdata() {

				getMainPlace({
					tenantId: this.$store.state.user.tenantId,
					lat: this.myLat,
					lon: this.myLng,
					type: this.type
				}).then(res => {

					this.markers = res;

					// if (this.pageNo == 0) {
					// 	this.markers = [];
					// 	this.pageNo++;
					// 	// uni.stopPullDownRefresh();

					// 	this.loadingStatus = 'loadmore';
					// } else {
					// 	if (!uni.$u.test.isEmpty(res)) {
					// 		this.pageNo++;

					// 		this.loadingStatus = 'loadmore';
					// 	} else {

					// 		this.loadingStatus = 'nomore';
					// 	}
					// }

					// if (!uni.$u.test.isEmpty(res)) {
					// 	this.markers = this.markers.concat(res);
					// }
				})

			},


			makephone(phone) {
				uni.makePhoneCall({
					phoneNumber: phone
				})
			},


			// itemClick(item) {
			// 	if (item.uid) {
			// 		uni.navigateTo({
			// 			url:'./chargedetail?distance=' + item.distance + "&uid=" + item.uid
			// 		})
			// 	}
			// },

			clickloadmore() {
				this.getsearchdata();
				this.showloading = true;
				this.loadingStatus = 'loading';
			},

			navi(item) {
				
				const lat = parseFloat(item.lat);
				const lng = parseFloat(item.lon);
				
				console.log('lat', lat);
				console.log('lng', lng);
				
				// uni.getLocation({
				// 	type: 'wgs84',
				// 	success: function(res) {
						uni.openLocation({ //​使用微信内置地图查看位置。
							latitude: bMapToQQMap(lat, lng)[0], //要去的纬度-地址
							longitude: bMapToQQMap(lat, lng)[1], //要去的经度-地址
							name: item.mainName,
							address: item.mainAddress
						})
				// 	},
				// 	fail:function(e){
				// 		console.log('失败了', e);
				// 	}
				// })
			},

		},
		onPullDownRefresh() {
			this.pageNo = 0;
			this.getsearchdata();
		},

		onReachBottom() {
			console.log('到底了');

			this.getsearchdata();
			this.showloading = true;
			this.loadingStatus = 'loading';
		}

	}
</script>

<style lang="scss" scoped>
	.title-wrap {
		display: inline-flex;
		justify-content: space-between;
	}

	.title {
		width: 100%;
		height: 80upx;
		line-height: 80upx;
		font-size: 28upx;
		color: $black33;
	}

	.title-warn {
		color: #E93131;
		background-color: #FFFFFF;
		white-space: nowrap;
		text-align: center;
		font-size: 26upx;
		line-height: 60upx;
		margin: 10upx 0upx;
		align-self: flex-end;
	}

	.navi-wrap {
		@include flex;
		align-items: center;
		justify-content: center;

		.navi-img {
			width: 30upx;
			height: 30upx;
			margin-right: 10upx;
		}
	}
</style>
